package com.bjpowernode.web.controller;


import com.bjpowernode.common.P2PConstants;
import com.bjpowernode.common.P2PKey;
import com.bjpowernode.common.ResultObject;
import com.bjpowernode.model.User;
import com.bjpowernode.service.BidInfoService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.aop.framework.AopProxy;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.context.annotation.*;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Repository;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.lang.reflect.InvocationHandler;
import java.math.BigDecimal;


@Controller
public class BidInfoController  {

    
    @DubboReference(interfaceClass = BidInfoService.class)
    private BidInfoService bidInfoService;

    @Resource
    private RedisTemplate redisTemplate;

    @PostMapping("/loan/invest")
    @ResponseBody
    public ResultObject invest(@RequestParam("loanId") Integer loanId,
                               @RequestParam("money")BigDecimal money,
                               HttpSession session){

        ResultObject ro =ResultObject.buildError();
        User user= (User) session.getAttribute(P2PConstants.SESSION_USER);
        if (loanId>0&&money.intValue()>0){
            //投资
            boolean result =bidInfoService.invest(loanId,money,user.getId());
            if (result){
                ro=new ResultObject(0,"投资成功");
                //投资成功，更新排行榜，使用redis做,资金累加方法
                redisTemplate.opsForZSet().incrementScore(P2PKey.BID_TOP_LIST,user.getPhone(),money.doubleValue());

            }
        }
        return ro;


    }
}
